package com.chinamobile.websocket.websocket.endpoint;

import com.chinamobile.websocket.domain.dto.MessageDto;
import com.chinamobile.websocket.util.SpringContextUtil;
import com.chinamobile.websocket.websocket.cache.EndpointCache;
import com.chinamobile.websocket.websocket.cache.EndpointImplCache;
import com.chinamobile.websocket.websocket.common.WebsocketConstant;
import com.chinamobile.websocket.websocket.scene.EndpointHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.io.IOException;
import java.util.Map;

/**
 * @className: WebSocketEndpointHandler
 * @description 节点
 * @author: fanzhijun
 * @date: 2024/1/18
 **/
public class WebSocketEndpointHandler extends TextWebSocketHandler {
    private static final Logger logger = LoggerFactory.getLogger(WebSocketEndpointHandler.class);
    private WebSocketSession session;
    String uniKey;
    Integer type;

    public void sendMessage(MessageDto messageDto) {
        try {
            TextMessage textMessage = new TextMessage(getEndpointHandler(getType()).getMessage(messageDto));
            synchronized (getSession()) {
                getSession().sendMessage(textMessage);
            }
        } catch (IOException e) {
            logger.error("error." + e.getMessage(), e);
        }
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        init(session);
    }

    private void init(WebSocketSession session) {
        WebSocketEndpointHandler webSocketEndpointHandler = new WebSocketEndpointHandler();
        Map<String, Object> map = session.getAttributes();
        webSocketEndpointHandler.setSession(session);
        webSocketEndpointHandler.setType(Integer.parseInt(String.valueOf(map.get(WebsocketConstant.TYPE))));
        webSocketEndpointHandler.setUniKey(String.valueOf(map.get(WebsocketConstant.UNIKEY)));
        EndpointCache.put(webSocketEndpointHandler);
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        getEndpointHandler(getType()).dealOnMessage(message.getPayload(), session);
    }

    private EndpointHandler getEndpointHandler(Integer type) {
        return SpringContextUtil.getApplicationContext().getBean(EndpointImplCache.getEndPointImplMap(type).getImpl());
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        EndpointCache.remove(session);
    }

    public WebSocketSession getSession() {
        return session;
    }

    public void setSession(WebSocketSession session) {
        this.session = session;
    }

    public String getUniKey() {
        return uniKey;
    }

    public void setUniKey(String uniKey) {
        this.uniKey = uniKey;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }
}
